package controller;

import java.sql.Connection;
import java.sql.ResultSet;
import java.sql.Statement;

import model.NhanVien;
import model.User;

public class UserController {

	public static boolean checkUser(String username, String password) {
		Connection conn = DBConection.MYSQL;
		if (conn == null)
			return false;
		try {

			Statement stmt = conn.createStatement();
			String sql = "SELECT * FROM user WHERE username='" + username
					+ "';";
			ResultSet rs = stmt.executeQuery(sql);
			User u;
			if (rs.next()) {
				u = new User();
				u.setUsername(username);
				u.setPassword(rs.getString("pass"));
				if (u.getPassword().equals(password))
					return true;
			}
			return false;
		} catch (Exception e) {
			e.printStackTrace();

			return false;
		}
	}

	public static User getUser(String name) {
		Connection conn = DBConection.MYSQL;
		if (conn == null)
			return null;
		try {

			Statement stmt = conn.createStatement();
			String sql = "SELECT * FROM user WHERE username='" + name + "';";
			ResultSet rs = stmt.executeQuery(sql);
			User u;
			if (rs.next()) {
				u = new User();
				u.setUsername(name);
				u.setPassword(rs.getString("pass"));
				u.setIdnv(rs.getInt("manv"));
				u.setPhanQuyen(rs.getString("phanquyen"));
				return u;
			}
			return null;
		} catch (Exception e) {
			e.printStackTrace();

			return null;
		}
	}

	public static void main(String[] args) {
		UserController u = new UserController();
		System.out.println(u.getUser("vokhanh").getIdnv());

	}
}
